package com.tencent.elife.asyn;

import android.os.Handler;
import android.os.Looper;

/**
 * @Description 带消息循环的工作线程
 * @author zijianlu
 */
public class WorkerThread extends Thread {

    private Handler mHandler;

    public WorkerThread() {
        start();
    }

    public synchronized Handler getHandler() {
        while (mHandler == null) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return mHandler;
    }

    public void run() {
        Looper.prepare();
        mHandler = new Handler();
        Looper.loop();
    }

    public synchronized void quit() {
        if (mHandler != null) {
            mHandler.getLooper().quit();
        }
    }
}
